package com.example.backend.service.impl;

import com.example.backend.entity.ProductLogistics;
import com.example.backend.mapper.ProductLogisticsMapper;
import com.example.backend.util.AuthUtil;
import com.example.backend.mapper.AccountMapper;
import com.example.backend.service.LogisticsService;
import com.example.backend.util.WeBASEUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web3j.utils.Numeric;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LogisticsServiceImpl implements LogisticsService {
    @Autowired
    private WeBASEUtils weBASEUtils;

    @Autowired
    private AuthUtil authUtil;
    
    @Autowired
    protected AccountMapper accountMapper;
    
    @Autowired
    private ProductLogisticsMapper productLogisticsMapper;

    @Override
    public String register(String username, String passWordF, String passwordS, String qualificationContent, int accountType) {
        return authUtil.register(username, passWordF, passwordS, qualificationContent, accountType);
    }
    
    @Override
    public String login(String accountId, String password, int accountType) {
        return authUtil.login(accountId, password, accountType);
    }
    
    @Override
    public String changePassword(String accountId, String oldPassword, String newPassword, int accountType) {
        return authUtil.changePassword(accountId, oldPassword, newPassword, accountType);
    }
    
    @Override
    public String postQualificationContent(String accountId, String qualificationContent, int accountType) {
        return authUtil.postQualificationContent(accountId, qualificationContent, accountType);
    }
    
    @Override
    public String postLogisticsInfo(Map<String, String> credentials){
        if (credentials.isEmpty()|| credentials.get("transactionId") == null|| credentials.get("accountId") == null) {
            return "参数不足或参数错误";
        }
        LocalDateTime now = LocalDateTime.now();


        ProductLogistics productLogistics = new ProductLogistics();
        productLogistics.setLogisticsId(credentials.get("transactionId"));
        productLogistics.setOperatorId(credentials.get("accountId"));
        productLogistics.setLogisticsCompany(credentials.get("logisticsCompany"));
        productLogistics.setDispatchTime(now);
        productLogisticsMapper.insert(productLogistics);
        //将时间转换成北京时间
        long epochSecond = now.toEpochSecond(ZoneOffset.ofHours(8)); // 北京时间
        String hexTimestamp = Numeric.toHexStringWithPrefix(BigInteger.valueOf(epochSecond));

        try {
            //数据上链(物流单号、账号、物流发出时间戳)
            List<Object> param = new ArrayList<>();
            param.add(credentials.get("transactionId"));
            param.add(credentials.get("logisticsCompany"));
            param.add(hexTimestamp);
            System.out.println(param);
            weBASEUtils.funcPost("LogisticsModule","uploadLogistics",param);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            return "提交物流信息成功";
        }



        
    }


}